//
//  MYOriginalView.swift
//  MYWeiBo
//
//  Created by apple on 16/5/16.
//  Copyright © 2016年 YanMo. All rights reserved.
//

import UIKit
import SDWebImage

class MYStatusOriginalView: UIView {
 //要给子控制赋值，则必须要有模型
    var status:MYStatus?{
        didSet {
            //设置头像
            if let urlString = status?.user?.profile_image_url{
                //取到头像的url
                let url = NSURL(string: urlString)
                iconView.sd_setImageWithURL(url, placeholderImage:
                UIImage(named: "avatar"))
            }
            //名称
            let name = status?.user?.screen_name
            nameLabel.text = name
            //时间
            timeLabel.text = status?.created_at
            //来源
            sourceLabel.text = status?.source
            //会员等级
            menberView.image = status?.user?.mbrankImage
            //认证类型
            verifiedView.image = status?.user?.verifiedImage
            //给文本赋值
            contentLabel.text = status?.text
        
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    
    private func prepareUI(){
        backgroundColor = UIColor.whiteColor()
        //添加子控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(menberView)
        addSubview(verifiedView)
        addSubview(contentLabel)
        //添加约束
        //用户头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(cellMargin)
            make.top.equalTo(self).offset(cellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        //用户名称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(cellMargin)
            make.top.equalTo(iconView)
        }
        //会员等级图标
        menberView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(cellMargin)
            make.centerY.equalTo(nameLabel)
            make.size.equalTo(CGSize(width: 14, height: 14))
            
        }
        
        //时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(nameLabel)
        }
       //来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(cellMargin)
            make.centerY.equalTo(timeLabel)
        }
        //认证图标
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        //文本内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(12)
        }
        //设置原创view的底部和contentlabel的底部重合偏移8
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentLabel).offset(cellMargin)
        }
        
    }
    
    
    //mark 懒加载
    //用户头像
    private lazy var iconView:UIImageView = UIImageView(image:UIImage(named: "avatar"))
    //用户名称
    private lazy var nameLabel:UILabel = {
        let label = UILabel()
        //大小
        label.font = UIFont.systemFontOfSize(NameFontSize)
        //颜色
        label.textColor = UIColor.darkGrayColor()
        return label
    }()
    //时间
    private lazy var timeLabel:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(TimeFontSize)
        label.textColor = UIColor.orangeColor()
        return label
    }()
    //来源
    private lazy var sourceLabel:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(TimeFontSize)
        label.textColor = UIColor.lightGrayColor()
        return label
    }()
    //会员德等级
      private lazy var menberView:UIImageView = UIImageView(image: nil)
    //认证类型
      private lazy var verifiedView:UIImageView = UIImageView(image: nil)
    //文本内容
    private lazy var contentLabel:UILabel = {
        let label = UILabel()
        //大小
        label.font = UIFont.systemFontOfSize(cellStatusFontSize)
        //颜色
        label.textColor = UIColor.darkGrayColor()
        //多行
        label.numberOfLines = 0
        //最大换行宽度
        label.preferredMaxLayoutWidth =  UIScreen.mainScreen().bounds.width - 2 * cellMargin
        return label
    }()
    
}






























